package com.svo.weitu.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.svo.platform.utils.PicUtil;
import com.svo.platform.utils.TimeConvert;
import com.svo.weitu.R;
import com.svo.weitu.model.MCol;
import com.svo.weitu.model.entity.ListEntity;
import com.svo.weitu.util.MyUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

public class CollectActivity extends AppCompatActivity implements OnItemClickListener{
	private ListView listView;
	private ArrayList<ListEntity> infos;
	private TextView noColTv;
	private Toolbar toolbar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collect);
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(R.mipmap.ic_back_normal);
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		listView = (ListView) findViewById(R.id.col_list);
		noColTv = (TextView) findViewById(R.id.no_col);
		listView.setOnItemClickListener(this);
		infos = new MCol(this).query();
		listView.setAdapter(new MyAdapter());
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		if (infos == null || infos.size() == 0) {
			noColTv.setVisibility(View.VISIBLE);
		}else {
			noColTv.setVisibility(View.GONE);
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
		final ListEntity entity = infos.get(arg2);
		String urls = entity.getUrls();
		String[] arrs = urls.split(";");
		ArrayList<String> pics = MyUtil.arrToList(arrs);
		new MyUtil().handlePics(entity, pics,this,infos);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	private class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = LayoutInflater.from(CollectActivity.this).inflate(R.layout.reader_list_item, null);
			ListEntity entity = infos.get(position);
			TextView timeView = (TextView) convertView.findViewById(R.id.display_time);
			TextView titleView = (TextView) convertView.findViewById(R.id.title);
			TextView loveNumTv = (TextView) convertView.findViewById(R.id.item_num_likes);
			timeView.setText(TimeConvert.formatDate(entity.getTime()));
			titleView.setText(entity.getTitle());
			loveNumTv.setVisibility(View.GONE);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.content_thumbnail);
			String thumbnail = entity.getThumbnail();
			if (TextUtils.isEmpty(thumbnail)) {
				String[] urls = entity.getUrls().split(";");
				PicUtil.displayImage(CollectActivity.this, urls[0], imageView);
			}else {
				PicUtil.displayImage(CollectActivity.this, thumbnail, imageView);
			}
			return convertView;
		}
	}
}
